package com.example.esp8266.Main.Main_Fragment;

import androidx.fragment.app.Fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.esp8266.R;
import com.example.esp8266.Login.SigninActivity;

import android.content.SharedPreferences;

public class User_Fragment extends Fragment
        implements View.OnClickListener{

    private View root;
    private int deviceCounts;
    private String userLevel,user_name_sql;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 获取传递的数据
        Bundle args = getArguments();
        if (args != null) {
            boolean loginState = args.getBoolean("login_state", false);
            deviceCounts = args.getInt("device_counts", 0);
            userLevel = args.getString("user_level", "访客模式");
            user_name_sql = args.getString("user_name_sql", "  用户名");
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.user_fragment, container, false);

        TextView exit = root.findViewById(R.id.exit_text);
        TextView user_name = root.findViewById(R.id.user_name_text);
        TextView device_number_text = root.findViewById(R.id.user_device_count_text);
        TextView user_level = root.findViewById(R.id.user_level_text);

        user_level.setText("  " + userLevel);
        device_number_text.setText("绑定设备 " + deviceCounts + " 台");
        user_name.setText(user_name_sql);
        //设置界面元素内容

        exit.setOnClickListener(this);//为退出按钮定义监听单击事件
        return root;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.exit_text) {
            // 创建一个新的Intent，从当前Fragment关联的Activity启动SigninActivity
            Intent intent = new Intent(getActivity(), SigninActivity.class);

            // 清除之前的所有Activity堆栈，确保登录界面是用户会见到的第一个界面
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

            // 检查 getActivity() 是否返回的对象为非空
            if (getActivity() != null) {
                // 获取 SharedPreferences 对象
                SharedPreferences sharedPreferences = getActivity().getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("login_state", false); // 初始化登录状态参数为 false
                editor.putBoolean("login_state_error", false); // 初始化登录状态参数为 false
                editor.putInt("device_counts", 0); // 初始化设备数量为 0
                editor.putString("user_level", "访客模式"); // 初始化用户等级为默认值
                editor.putString("password", null); // 初始化用户密码
                editor.putString("server_ip", null); // 初始化服务器
                editor.putString("server_port", null); // 初始化服务器地址
                for (int i = 0; i < 3; i++) {
                    editor.putString("collection_" + (i + 1), null);//初始化绑定设备
                }
                editor.apply();
            } else {
                Log.e("Fragment", "getActivity() returned null");
                // 可以选择在这里处理 getActivity() 返回 null 的情况，例如显示错误信息或采取其他操作
            }
            // 启动SigninActivity
            startActivity(intent);
        }
    }
}

